import os
import shutil
from abc import abstractmethod
from django.apps import apps
from django.db import models
from django.template.defaultfilters import slugify
from django.utils import timezone
from rest_framework.exceptions import ValidationError

from DataAnalysisManager.models import UserScore
from DataAnalysisManager.serializers import UserScoreSerializer
from HopeIESystem.settings import MEDIA_ROOT
# Create your models here.
"""
创建一个课程模型
"""


class CourseSelection(models.Model):
    # 学年
    year_choices = [(str(i) + '-' + str(i + 1) + "学年", str(i) + '-' + str(i + 1) + "学年") for i in range(2023, 2066)]
    year = models.CharField(verbose_name="学年", max_length=255, choices=year_choices,
                            default=str(2023) + '-' + str(2023 + 1) + "学年")
    # 学期
    semester = models.CharField(verbose_name="学期", max_length=255,
                                choices=[('第一学期', '第一学期'), ('第二学期', '第二学期')], default='第一学期')
    # 年级
    grade_choices = [(str(i) + "级", str(i) + "级") for i in range(2019, 2066)]
    grade = models.CharField(verbose_name="年级", max_length=255, choices=grade_choices, default=str(2021) + "级")
    # 专业
    major = models.CharField(verbose_name="专业", max_length=255)
    # 班级
    class_name = models.CharField(verbose_name="班级", max_length=255)
    # 课程名
    course_name = models.CharField(verbose_name="课程名", max_length=255)
    # 课程号
    course_id = models.CharField(verbose_name="课程号", max_length=255)
    # 课时
    course_hour = models.CharField(verbose_name="课时", max_length=255)
    # 任教老师（账号）
    teacher_account = models.CharField(verbose_name="任教老师账号", max_length=255, default=None, null=True)
    # 任教老师名
    teacher_name = models.CharField(verbose_name="任教老师名", max_length=255, default=None,null=True)
    # 允许选课
    is_open = models.BooleanField(verbose_name="是否开放选课", default=True)

    def save(self, *args, **kwargs):
        # 如果teacher_name和teacher_account 为空则is_open为True反之为false
        if self.teacher_name is not None and self.teacher_account is not None:
            self.is_open = False
        # 对于所有字段 如果为 'nan' 一律看作None
        for field in self._meta.get_fields():
            if getattr(self, field.name) == 'nan':
                setattr(self, field.name, None)


        super().save(*args, **kwargs)  # 保存实例以生成ID



"""
创建一个模型去记录当前的用户的选课时间段
以及是否选完
"""


class SelectionInfo(models.Model):
    # 用户账号
    author_account = models.CharField(verbose_name="用户账号", max_length=255, unique=True)
    # 用户姓名
    author_name = models.CharField(verbose_name="用户名", max_length=255)
    # 选课开始时间
    start_time = models.DateTimeField(verbose_name="选课开始时间")
    # 选课结束时间
    end_time = models.DateTimeField(verbose_name="选课结束时间")
    # 是否选完
    is_finish = models.BooleanField(verbose_name="是否选完", default=False)
